using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Globals;
using Presentation.Contracts;
using Presentation;

namespace ProjektZespolowy.Friends
{
    public partial class Invitations : System.Web.UI.Page, IFriendsView
    {
        #region Members

        /// <summary>
        /// Instance of presenter.
        /// </summary>
        private FriendsPresenter presenter;

        #endregion Members

        #region Override Methods

        /// <summary>
        /// Raises the <see cref="E:System.Web.UI.Control.Init"></see> event.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs"></see> object that contains the event data.</param>
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            presenter = new FriendsPresenter(this);
        }

        #endregion Override Methods

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
              
                    b1_2a.Visible = false;
                    b2_2a.Visible = false;
                    b3_2a.Visible = false;
                    b4_2a.Visible = false;
                    b5_2a.Visible = false;
                    b6_2a.Visible = false;

                    b1_2b.Visible = false;
                    b2_2b.Visible = false;
                    b3_2b.Visible = false;
                    b4_2b.Visible = false;
                    b5_2b.Visible = false;
                    b6_2b.Visible = false;

                    int numberOfPagesRec = presenter.FindNrOfPagesOfReceivedInv();
                    Session["nrOfPagesRec"] = numberOfPagesRec;
                    int numberOfPagesSent = presenter.FindNrOfPagesOfSentInv();
                    Session["nrOfPagesSent"] = numberOfPagesSent;

                    if (numberOfPagesRec <= 6)
                    {
                        this.InitPage1_2a(numberOfPagesRec);  
                        presenter.FindReceivedInvitationsPerPage(1);
                      
                    }
                    else
                    {
                        this.InitPage2_2a();
                        presenter.FindReceivedInvitationsPerPage(1);
                    }

                    if (numberOfPagesSent <= 6)
                    {
                        this.InitPage1_2b(numberOfPagesSent);
                        presenter.FindSentInvitationsPerPage(1);

                    }
                    else
                    {
                        this.InitPage2_2b();
                        presenter.FindSentInvitationsPerPage(1);
                    }

                    presenter.SetCaption();
                }


          }


        public void InitPage1_2a(int numberOfPages)
        {
            int i_tmp = 0;

                  for (int i = 1; i <= numberOfPages; i++)
                        {
                            for (int j = 1; j <= i; j++)
                            {
                                if (j > 1)
                                    i_tmp--;
                                else
                                    i_tmp = i;

                                Button b_tmp = (Button)UpdatePanel2a.FindControl("b" + j.ToString()+"_2a");
                                b_tmp.Text = i_tmp.ToString();
                                b_tmp.Visible = true;

                                if (i == numberOfPages && j == i)
                                {
                                    b_tmp = (Button)UpdatePanel2a.FindControl("b" + j.ToString()+"_2a");
                                    b_tmp.Enabled = false;
                                }

                            }
                        }
        }

        public void InitPage1_2b(int numberOfPages)
        {
            int i_tmp = 0;

            for (int i = 1; i <= numberOfPages; i++)
            {
                for (int j = 1; j <= i; j++)
                {
                    if (j > 1)
                        i_tmp--;
                    else
                        i_tmp = i;

                    Button b_tmp = (Button)UpdatePanel2b.FindControl("b" + j.ToString() + "_2b");
                    b_tmp.Text = i_tmp.ToString();
                    b_tmp.Visible = true;

                    if (i == numberOfPages && j == i)
                    {
                        b_tmp = (Button)UpdatePanel2b.FindControl("b" + j.ToString()+"_2b");
                        b_tmp.Enabled = false;
                    }

                }
            }
        }

        public void InitPage2_2a()
        {
            b1_2a.Text = "->";
            b1_2a.CommandName = "Next";
            b1_2a.Visible = true;
            b2_2a.Text = "5";
            b2_2a.Visible = true;
            b3_2a.Text = "4";
            b3_2a.Visible = true;
            b4_2a.Text = "3";
            b4_2a.Visible = true;
            b5_2a.Text = "2";
            b5_2a.Visible = true;
            b6_2a.Text = "1";
            b6_2a.Enabled = false;
            b6_2a.Visible = true;
        }

        public void InitPage2_2b()
        {
            b1_2b.Text = "->";
            b1_2b.CommandName = "Next";
            b1_2b.Visible = true;
            b2_2b.Text = "5";
            b2_2b.Visible = true;
            b3_2b.Text = "4";
            b3_2b.Visible = true;
            b4_2b.Text = "3";
            b4_2b.Visible = true;
            b5_2b.Text = "2";
            b5_2b.Visible = true;
            b6_2b.Text = "1";
            b6_2b.Enabled = false;
            b6_2b.Visible = true;
        }
  
        public string FormatURL(object _id)
   {
       int num = Convert.ToInt32(_id);

       return ("~/GetUserPicture.aspx?id=" + _id);

   }

        protected void DataList2a_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item ||
                 e.Item.ItemType == ListItemType.AlternatingItem)
            {
                HyperLink hp = (HyperLink)e.Item.FindControl("HyperLink2a");
                hp.Text = Convert.ToString(DataBinder.Eval(e.Item.DataItem, "login"));
                
                ImageButton img = (ImageButton)e.Item.FindControl("IAvatarFriend2a");

                img.Height = 80;
                img.Width = 80;
                img.ImageUrl = FormatURL(DataBinder.Eval(e.Item.DataItem,"id"));
                hp.NavigateUrl = "Friends.aspx?id=" + DataBinder.Eval(e.Item.DataItem,"id").ToString();
                img.CommandName = DataBinder.Eval(e.Item.DataItem,"id").ToString();

                Button buttonA = (Button)e.Item.FindControl("bAccept");
                Button buttonR = (Button)e.Item.FindControl("bRefuse");
               
                buttonA.CommandName = DataBinder.Eval(e.Item.DataItem, "id").ToString();
                buttonR.CommandName = DataBinder.Eval(e.Item.DataItem, "id").ToString();          
           }          
       }

        protected void DataList2b_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item ||
                 e.Item.ItemType == ListItemType.AlternatingItem)
            {
                HyperLink hp = (HyperLink)e.Item.FindControl("HyperLink2b");
                hp.Text = Convert.ToString(DataBinder.Eval(e.Item.DataItem, "login"));

                ImageButton img = (ImageButton)e.Item.FindControl("IAvatarFriend2b");

                img.Height = 80;
                img.Width = 80;
                img.ImageUrl = FormatURL(DataBinder.Eval(e.Item.DataItem, "id"));
                hp.NavigateUrl = "Friends.aspx?id=" + DataBinder.Eval(e.Item.DataItem, "id").ToString();
                img.CommandName = DataBinder.Eval(e.Item.DataItem, "id").ToString();

                Button buttonC = (Button)e.Item.FindControl("bCancelInv");
                buttonC.CommandName = DataBinder.Eval(e.Item.DataItem, "id").ToString();
            }
        }

        protected void IAvatarFriend2a_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton img = sender as ImageButton;
            
            Response.Redirect("/Friends/Friends.aspx?id="+img.CommandName);
        }

        protected void IAvatarFriend2b_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton img = sender as ImageButton;

            Response.Redirect("/Friends/Friends.aspx?id=" + img.CommandName);
        }


        protected void b6_2a_Click(object sender, EventArgs e)
        {
            if (b6_2a.CommandName == "Prev")
            {
                int nr_str=0;
                int idLastButton = 0;
                for (int i = 2; i <= 5; i++)
                {
                    Button b_disp = (Button)UpdatePanel2a.FindControl("b" + i.ToString()+"_2a");

                    if (b_disp.Enabled == false)
                    {
                        nr_str = int.Parse(b_disp.Text);
                        idLastButton = i;
                    }
                }

                if (idLastButton == 5)
                {
                    for (int i = 2; i <= 5; i++)
                    {
                        Button b_disp = (Button)UpdatePanel2a.FindControl("b" + i.ToString()+"_2a");

                        b_disp.Text = ((int)(nr_str - (i-1))).ToString();

                        b_disp.Visible = true;
                        b_disp.Enabled = true;

                    }

                    b1_2a.Visible = true;
                    
                    if (int.Parse(b5_2a.Text) == 2)
                    {
                        b6_2a.Text = "1";
                        b6_2a.CommandName = "";
                    }

                    b2_2a.Enabled = false;
             
                   
                    presenter.FindReceivedInvitationsPerPage(int.Parse(b2_2a.Text));

                }
                else
                {
                    for (int i = 2; i <= 5; i++)
                    {
                        Button b_temp = (Button)UpdatePanel2a.FindControl("b" + i.ToString()+"_2a");
                        if (i == idLastButton)
                        {
                            b_temp.Enabled = true;
                            b_temp = (Button)UpdatePanel2a.FindControl("b" + (i + 1).ToString()+"_2a");
                            b_temp.Enabled = false;

                            presenter.FindReceivedInvitationsPerPage(int.Parse(b_temp.Text));

                        }
                    }
                }
            }
            else
            {
                b6_2a.Enabled = false;
                b1_2a.Enabled = true;
                b2_2a.Enabled = true;
                b3_2a.Enabled = true;
                b4_2a.Enabled = true;
                b5_2a.Enabled = true;
            
                presenter.FindReceivedInvitationsPerPage(int.Parse(b6_2a.Text));
            }
          
        }

        protected void b5_2a_Click(object sender, EventArgs e)
        {
            b1_2a.Enabled = true;
            b2_2a.Enabled = true;
            b3_2a.Enabled = true;
            b4_2a.Enabled = true;
            b5_2a.Enabled = false;
            b6_2a.Enabled = true;
        
            presenter.FindReceivedInvitationsPerPage(int.Parse(b5_2a.Text));

        }

        protected void b4_2a_Click(object sender, EventArgs e)
        {
            b1_2a.Enabled = true;
            b2_2a.Enabled = true;
            b3_2a.Enabled = true;
            b4_2a.Enabled = false;
            b5_2a.Enabled = true;
            b6_2a.Enabled = true;
        
            presenter.FindReceivedInvitationsPerPage(int.Parse(b4_2a.Text));

        }

        protected void b3_2a_Click(object sender, EventArgs e)
        {
            b1_2a.Enabled = true;
            b2_2a.Enabled = true;
            b3_2a.Enabled = false;
            b4_2a.Enabled = true;
            b5_2a.Enabled = true;
            b6_2a.Enabled = true;
          
           
            presenter.FindReceivedInvitationsPerPage(int.Parse(b3_2a.Text));

        }

        protected void b2_2a_Click(object sender, EventArgs e)
        {
            b1_2a.Enabled = true;
            b2_2a.Enabled = false;
            b3_2a.Enabled = true;
            b4_2a.Enabled = true;
            b5_2a.Enabled = true;
            b6_2a.Enabled = true;
          
            presenter.FindReceivedInvitationsPerPage(int.Parse(b2_2a.Text));

        }

        protected void b1_2a_Click(object sender, EventArgs e)
        {
            if (b1_2a.CommandName == "Next")
            {
                int nr_str;
                int idLastButton = 0;
                for (int i = 2; i <= 6; i++)
                {
                    Button b_disp = (Button)UpdatePanel2a.FindControl("b" + i.ToString()+"_2a");

                    if (b_disp.Enabled == false)
                    {
                        nr_str = int.Parse(b_disp.Text);
                        idLastButton = i;
                    }
                }

                if (idLastButton == 2)
                {
                    if (b6_2a.Text.CompareTo("1") == 0)
                    {
                        b6_2a.Text = "<-";
                        b6_2a.CommandName = "Prev";
                    }
                    for (int i = 5; i >= 2; i--)
                    {
                        Button b_disp = (Button)UpdatePanel2a.FindControl("b" + i.ToString()+"_2a");

                        if ((int.Parse(b_disp.Text) + 4) <= (int)Session["nrOfPagesRec"])
                            b_disp.Text = ((int)(int.Parse(b_disp.Text) + 4)).ToString();
                        else
                        {
                            b_disp.Visible = false;
                            b1_2a.Visible = false;

                        }
                        b_disp.Enabled = true;

                    }

                    if (int.Parse(b2_2a.Text) == (int)Session["nrOfPagesRec"])
                        b1_2a.Visible = false;

                    b5_2a.Enabled = false;
                 
                    presenter.FindReceivedInvitationsPerPage(int.Parse(b5_2a.Text));

                }
                else
                {
                    for (int i = 2; i <= 6; i++)
                    {
                        Button b_temp = (Button)UpdatePanel2a.FindControl("b" + i.ToString()+"_2a");
                        if (i == idLastButton)
                        {
                            b_temp.Enabled = true;
                            b_temp = (Button)UpdatePanel2a.FindControl("b" + (i - 1).ToString()+"_2a");
                            b_temp.Enabled = false;
                       
                            presenter.FindReceivedInvitationsPerPage(int.Parse(b_temp.Text));

                        }
                    }
                }
            }
            else
            {
                b1_2a.Enabled = false;
                b2_2a.Enabled = true;
                b3_2a.Enabled = true;
                b4_2a.Enabled = true;
                b5_2a.Enabled = true;
                b6_2a.Enabled = true;

               presenter.FindReceivedInvitationsPerPage(int.Parse(b1_2a.Text));
            }
          

        }

        protected void b6_2b_Click(object sender, EventArgs e)
        {
            if (b6_2b.CommandName == "Prev")
            {
                int nr_str = 0;
                int idLastButton = 0;
                for (int i = 2; i <= 5; i++)
                {
                    Button b_disp = (Button)UpdatePanel2b.FindControl("b" + i.ToString()+"_2b");

                    if (b_disp.Enabled == false)
                    {
                        nr_str = int.Parse(b_disp.Text);
                        idLastButton = i;
                    }
                }

                if (idLastButton == 5)
                {
                    for (int i = 2; i <= 5; i++)
                    {
                        Button b_disp = (Button)UpdatePanel2b.FindControl("b" + i.ToString()+"_2b");

                        b_disp.Text = ((int)(nr_str - (i - 1))).ToString();

                        b_disp.Visible = true;
                        b_disp.Enabled = true;

                    }

                    b1_2b.Visible = true;

                    if (int.Parse(b5_2b.Text) == 2)
                    {
                        b6_2b.Text = "1";
                        b6_2b.CommandName = "";
                    }

                    b2_2b.Enabled = false;


                    presenter.FindSentInvitationsPerPage(int.Parse(b2_2b.Text));

                }
                else
                {
                    for (int i = 2; i <= 5; i++)
                    {
                        Button b_temp = (Button)UpdatePanel2b.FindControl("b" + i.ToString()+"_2b");
                        if (i == idLastButton)
                        {
                            b_temp.Enabled = true;
                            b_temp = (Button)UpdatePanel2b.FindControl("b" + (i + 1).ToString()+"_2b");
                            b_temp.Enabled = false;

                            presenter.FindSentInvitationsPerPage(int.Parse(b_temp.Text));

                        }
                    }
                }
            }
            else
            {
                b6_2b.Enabled = false;
                b1_2b.Enabled = true;
                b2_2b.Enabled = true;
                b3_2b.Enabled = true;
                b4_2b.Enabled = true;
                b5_2b.Enabled = true;

                presenter.FindSentInvitationsPerPage(int.Parse(b6_2b.Text));
            }

        }

        protected void b5_2b_Click(object sender, EventArgs e)
        {
            b1_2b.Enabled = true;
            b2_2b.Enabled = true;
            b3_2b.Enabled = true;
            b4_2b.Enabled = true;
            b5_2b.Enabled = false;
            b6_2b.Enabled = true;

            presenter.FindSentInvitationsPerPage(int.Parse(b5_2b.Text));

        }

        protected void b4_2b_Click(object sender, EventArgs e)
        {
            b1_2b.Enabled = true;
            b2_2b.Enabled = true;
            b3_2b.Enabled = true;
            b4_2b.Enabled = false;
            b5_2b.Enabled = true;
            b6_2b.Enabled = true;

            presenter.FindSentInvitationsPerPage(int.Parse(b4_2b.Text));

        }

        protected void b3_2b_Click(object sender, EventArgs e)
        {
            b1_2b.Enabled = true;
            b2_2b.Enabled = true;
            b3_2b.Enabled = false;
            b4_2b.Enabled = true;
            b5_2b.Enabled = true;
            b6_2b.Enabled = true;


            presenter.FindSentInvitationsPerPage(int.Parse(b3_2b.Text));

        }

        protected void b2_2b_Click(object sender, EventArgs e)
        {
            b1_2b.Enabled = true;
            b2_2b.Enabled = false;
            b3_2b.Enabled = true;
            b4_2b.Enabled = true;
            b5_2b.Enabled = true;
            b6_2b.Enabled = true;

            presenter.FindSentInvitationsPerPage(int.Parse(b2_2b.Text));

        }

        protected void b1_2b_Click(object sender, EventArgs e)
        {
            if (b1_2b.CommandName == "Next")
            {
                int nr_str;
                int idLastButton = 0;
                for (int i = 2; i <= 6; i++)
                {
                    Button b_disp = (Button)UpdatePanel2b.FindControl("b" + i.ToString()+"_2b");

                    if (b_disp.Enabled == false)
                    {
                        nr_str = int.Parse(b_disp.Text);
                        idLastButton = i;
                    }
                }

                if (idLastButton == 2)
                {
                    if (b6_2b.Text.CompareTo("1") == 0)
                    {
                        b6_2b.Text = "<-";
                        b6_2b.CommandName = "Prev";
                    }
                    for (int i = 5; i >= 2; i--)
                    {
                        Button b_disp = (Button)UpdatePanel2b.FindControl("b" + i.ToString()+"_2b");

                        if ((int.Parse(b_disp.Text) + 4) <= (int)Session["nrOfPagesSent"])
                            b_disp.Text = ((int)(int.Parse(b_disp.Text) + 4)).ToString();
                        else
                        {
                            b_disp.Visible = false;
                            b1_2b.Visible = false;

                        }
                        b_disp.Enabled = true;

                    }

                    if (int.Parse(b2_2b.Text) == (int)Session["nrOfPagesSent"])
                        b1_2b.Visible = false;

                    b5_2b.Enabled = false;

                    presenter.FindSentInvitationsPerPage(int.Parse(b5_2b.Text));

                }
                else
                {
                    for (int i = 2; i <= 6; i++)
                    {
                        Button b_temp = (Button)UpdatePanel2b.FindControl("b" + i.ToString()+"_2b");
                        if (i == idLastButton)
                        {
                            b_temp.Enabled = true;
                            b_temp = (Button)UpdatePanel2b.FindControl("b" + (i - 1).ToString()+"_2b");
                            b_temp.Enabled = false;

                            presenter.FindSentInvitationsPerPage(int.Parse(b_temp.Text));

                        }
                    }
                }
            }
            else
            {
                b1_2b.Enabled = false;
                b2_2b.Enabled = true;
                b3_2b.Enabled = true;
                b4_2b.Enabled = true;
                b5_2b.Enabled = true;
                b6_2b.Enabled = true;

                presenter.FindSentInvitationsPerPage(int.Parse(b1_2b.Text));
            }


        }


        #region IFriendsView Members

        public void ShowFriends(DataTable dt,int v)
        {
            if (v == 1)
            {
                ViewState["tabela1"] = dt;
                DataList2a.DataSource = ViewState["tabela1"];
                DataList2a.DataBind();
            }
            else if (v == 2)
            {
                ViewState["tabela2"] = dt;
                DataList2b.DataSource = ViewState["tabela2"];
                DataList2b.DataBind();
            }
        }

        public string StrID()
        {
            string strID = Session[ContentNamesGlobals.SessionWithUserId].ToString();
            
            return strID;
        }

        public string SessionID()
        {
            string strID = Session[ContentNamesGlobals.SessionWithUserId].ToString();

            return strID;
        }

        public void SetHeader(string userId)
        {
            Label lh2a = (Label)View2.FindControl("lHeader2a");
            Label lh2b = (Label)View2.FindControl("lHeader2b");
            lh2a.Text = "Zaproszenia do znajomosci ("+presenter.GetNumberOfAllReceivedInvitations().ToString()+")";
            lh2b.Text = "Wyslane zaproszenia ("+presenter.GetNumberOfAllSentInvitations().ToString()+")";
        }

        #endregion


        protected void bCancelInv_Click(object sender, EventArgs e)
        {
            Button b = sender as Button;

            presenter.DelFriend(int.Parse(b.CommandName));
            Response.Redirect("/Friends/Invitations.aspx");
            //presenter.FindSentInvitationsPerPage(1);

        }

        protected void bAccept_Click(object sender, EventArgs e)
        {
            Button b = sender as Button;
            presenter.AcceptInvitation(int.Parse(b.CommandName));
            Response.Redirect("/Friends/Invitations.aspx");
           // presenter.FindReceivedInvitationsPerPage(1);
        }

        protected void bRefuse_Click(object sender, EventArgs e)
        {
            Button b = sender as Button;
            presenter.RefuseInvitation(int.Parse(b.CommandName));
            Response.Redirect("/Friends/Invitations.aspx");
          //  presenter.FindReceivedInvitationsPerPage(1);
        }


        
     
    }

}
   